<template>
  <div class="app-container">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.keyword"
          placeholder="品牌名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="success" @click="resetData()">重置</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column
        prop="id"
        align="center"
        label="品牌id"
      ></el-table-column>
      <el-table-column
        prop="name"
        align="center"
        label="品牌名"
      ></el-table-column>
      <el-table-column prop="logo" align="center" label="品牌logo地址">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 60px"
            :src="scope.row.logo"
            fit="fill"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="description"
        align="center"
        label="介绍"
      ></el-table-column>
      <el-table-column
        prop="firstLetter"
        align="center"
        label="检索首字母"
      ></el-table-column>
      <el-table-column
        prop="sort"
        align="center"
        label="排序"
      ></el-table-column>
      <el-table-column fixed="right" align="center" width="250" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[8, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next,sizes, jumper"
    ></el-pagination>
  </div>
</template>

<script>
import AddOrUpdate from "./add-or-update";
import BrandApi from "@/api/product/brand";
import { pageCompose } from "@/components/mixin";

export default {
  components: {
    AddOrUpdate,
  },
  mixins: [pageCompose],
  data() {
    return {
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 8,
      total: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      dataListSelections: [],
      relationVisible: false,
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    resetData() {
      this.dataForm = {};
      this.getDataList();
    },
    getDataList() {
      this.dataListLoading = true;
      BrandApi.getDataList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        ...this.dataForm,
      }).then(({ data }) => {
        this.dataList = data.list;
        this.total = data.totalCount;
        this.dataListLoading = false;
      });
    },
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          BrandApi.deleteBrand(ids).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          });
        })
        .catch(() => {});
    },
  },
};
</script>
